<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests\SystemRequest;
use App\Http\Controllers\Controller;
use App\Model\System;
use DB,Input;

class SystemController extends Controller
{
    protected $cates = [
            0=>'基本配置',
            1=>'SEO配置',
            2=>'其他配置'
        ];
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $sysconfigs = System::getConfigs();
//        dd($sysconfigs);
        return view('admin.system.index', compact('sysconfigs'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.system.add', ['cates'=>$this->cates]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(SystemRequest $request)
    {
        $data = $request->except('_token');
        $res = System::insert($data);
        if(false !== $res){
            $this->putConfig();
            return redirect('/admin/system');
        } else {
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // 获取配置信息
        $data = System::find($id);
        return view('admin.system.edit', ['data'=>$data,'cates'=>$this->cates]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // 获取信息
        $data = $request->except('_token', '_method');
        $res = System::where('id', $id)->update($data);
        if(false !== $res){
            $this->putConfig();
            return redirect('/admin/system/all');
        } else {
            return back()->with(['error'=>'修改失败']);
        }
    }
    
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = System::where('id', $id)->delete();
        if(false !== $res){
            $this->putConfig();
            return response()->json(['status'=>1, 'info'=>'删除成功']);
        } else {
            return response()->json(['status'=>0, 'info'=>'删除失败']);
        }
    }
    
    // 写入配置文件
    private function putConfig()
    {
        $configs = System::get()->pluck('value', 'key')->all();
        $configs = var_export($configs, true);  // 将数组转换为字符串
        $str = "<?php\n return " . $configs . ';';
        $path = config_path() . '/web.php';
        
        file_put_contents($path, $str);
    }
    
    // 获取配置项列表
    public function getConfigs(Request $request)
    {
        $request->flashOnly('title');
        $configs = System::orderBy('pid', 'asc')
                ->orderBy('sort', 'asc')
                ->where('title', 'like', '%' . $request->get('title', '') . '%')
                ->paginate(15);
        
        foreach($configs->all() as $v){
            if($v->type == 'radio'){
                $value = explode(',', $v->value_range);
                $kv = [];
                foreach($value as $vv){
                    $tmp = explode('|', $vv);
                    $kv[$tmp[0]] = $tmp[1];
                }
                $v->kv = $kv;
            }
        }
        
        return view('admin.system.list', compact('configs'));
    }
    
    // 保存
    public function save(Request $request)
    {
        $data = $request->except('_token');
        $system = new System();
        foreach($data as $k=>$v){
           $system->where('key', $k)->update(['value'=>$v]);
        }
        $this->putConfig();
        return back()->with(['info'=>'更新成功']);
    }
}
